package com.mall4j.cloud.product.service.impl;

import com.mall4j.cloud.api.product.vo.BrandVO;
import com.mall4j.cloud.api.product.vo.CategoryVO;
import com.mall4j.cloud.api.product.vo.SpuVO;
import com.mall4j.cloud.common.constant.StatusEnum;
import com.mall4j.cloud.common.database.dto.PageDTO;
import com.mall4j.cloud.common.database.util.PageUtil;
import com.mall4j.cloud.common.database.vo.PageVO;
import com.mall4j.cloud.product.dto.BrandDTO;
import com.mall4j.cloud.product.dto.CategoryBrandDTO;
import com.mall4j.cloud.product.dto.SpuDTO;
import com.mall4j.cloud.product.mapper.BrandMapper;
import com.mall4j.cloud.product.mapper.CategoryBrandMapper;
import com.mall4j.cloud.product.mapper.CategoryMapper;
import com.mall4j.cloud.product.mapper.SpuMapper;
import com.mall4j.cloud.product.model.Brand;
import com.mall4j.cloud.product.service.BrandService;
import com.mall4j.cloud.product.vo.CategoryBrandVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * 品牌
 *
 * @author JCM
 * @date 2021-04-24 21:02:00
 */
@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;

    @Autowired
    private CategoryBrandMapper categoryBrandMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public PageVO<BrandVO> page(PageDTO pageDTO, BrandDTO brandDTO) {
        return PageUtil.doPage(pageDTO, () -> brandMapper.list(brandDTO));
    }

    @Override
    public BrandVO getByBrandId(Long brandId) {
        BrandVO brandVO = brandMapper.getByBrandId(brandId);

        //设置其余字段
        CategoryBrandDTO categoryBrandDTO = new CategoryBrandDTO();
        categoryBrandDTO.setBrandId(brandId);
        List<CategoryBrandVO> categoryBrandVOS = categoryBrandMapper.list(categoryBrandDTO);
        List<CategoryVO> categoryVOS = new ArrayList<>();
        for (CategoryBrandVO categoryBrandVO : categoryBrandVOS){
            List<String> pathNames = new ArrayList<>();
            CategoryVO categoryVO = categoryMapper.getByCategoryId(categoryBrandVO.getCategoryId());
            //找到它的所有父类
            String path = categoryVO.getPath();
            String[] split = path.split("-");
            for (int i = 1; i < split.length; i++){
                long ancestorId = Long.parseLong(split[i]);
                CategoryVO ancestorCategoryVO = categoryMapper.getByCategoryId(ancestorId);
                pathNames.add(ancestorCategoryVO.getName());
            }
            categoryVO.setPathNames(pathNames);
            categoryVOS.add(categoryVO);
        }
        brandVO.setCategories(categoryVOS);
        return brandVO;
    }

    @Override
    public List<BrandVO> getByCategoryId(Long categoryId) {
        List<BrandVO> brandVOS = new ArrayList<>();
        List<CategoryBrandVO> categoryBrandVOS = categoryBrandMapper.getByCategoryId(categoryId);
        for (CategoryBrandVO categoryBrandVO : categoryBrandVOS){
            BrandVO brandVO = brandMapper.getByBrandId(categoryBrandVO.getBrandId());
            if(brandVO.getStatus() == StatusEnum.ENABLE.value()) {
                brandVOS.add(brandVO);
            }
        }
        return brandVOS;
    }

    @Override
    public void save(Brand brand) {
        //保存时默认设置品牌状态为启用
        brand.setStatus(StatusEnum.ENABLE.value());
        brandMapper.save(brand);
    }

    @Override
    public void update(Brand brand) {
        brandMapper.update(brand);
    }

    @Transactional
    @Override
    public void deleteById(Long brandId) {
        //删除品牌时删除其相关表的数据
        categoryBrandMapper.deleteByBrandId(brandId);
        brandMapper.deleteById(brandId);
    }
}
